package com.cskaoyan.factory;

import com.cskaoyan.bean.Car;
import lombok.Data;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

/**
 * @author stone
 * @date 2021/12/25 11:50
 */
@Data
public class CarProxyFactory {
    static Car car; //让其来源于容器
    public static Car createProxy() {
        //Car使用什么动态代理 → 没有接口 cglib
        //cglib不需要额外引入 spring依赖中已经包含
        Object carProxy = Enhancer.create(Car.class, new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                method.invoke(car, objects);
                System.out.println("溜了溜了");
                return null;
            }
        });
        return (Car) carProxy;
    }
}
